package cn.wildfire.chat.kit.user;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.Collections;

import butterknife.BindView;
import cn.wildfire.chat.app.extendsion.ui.ToastTip;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.common.OperateResult;
import cn.wildfirechat.model.ModifyMyInfoEntry;
import cn.wildfirechat.model.UserInfo;

import static cn.wildfirechat.model.ModifyMyInfoType.Modify_DisplayName;

/**
 * @Author LiZhong
 * @Date 2019/9/7
 * TODO 修改昵称
 */
public class ChangeNameActivity extends WfcBaseActivity {


    @BindView(R2.id.nameEditText)
    EditText nameEditText;
    private UserViewModel userViewModel;
    private UserInfo userInfo;

    @Override
    protected void afterViews() {
        super.afterViews();
        userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);
        userInfo = userViewModel.getUserInfo(userViewModel.getUserId(), false);
        if (userInfo == null) {
            Toast.makeText(this, getString(R.string.user_unexist), Toast.LENGTH_SHORT).show();
            finish();
        }
        initView2();
    }

    @Override
    protected int contentLayout() {
        return R.layout.user_change_account_activity;
    }

    private void initView2() {
        try {
            if (userInfo != null) {
                nameEditText.setText(userInfo.displayName);
            }
            nameEditText.setSelection(nameEditText.getText().toString().trim().length());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //nameEditText.addTextChangedListener(new MaxLengthWatcher(16, nameEditText));
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 3);
        setReplaceTitle(null, R.string.modify_nickname);
        setSearchLayoutVisibility(false);
        getRightTxtView().setBackgroundResource(R.drawable.commom_green_radiusmuch);
        getRightTxtView().setTextColor(getResources().getColor(R.color.white));
        setTextThenBinClickForRightTextView(View.VISIBLE, getString(R.string.complete), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeAccount();
            }
        });
    }


    private void changeAccount() {
        String nickName = nameEditText.getText().toString().trim();
        if (TextUtils.isTextEmpty(nickName)) {
            ToastTip.tip(getString(R.string.nick_name_cannot_be_null));
            return;
        }
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content(getString(R.string.modifying))
                .progress(true, 100)
                .build();
        dialog.show();
        ModifyMyInfoEntry entry = new ModifyMyInfoEntry(Modify_DisplayName, nickName);
        userViewModel.modifyMyInfo(Collections.singletonList(entry)).observe(this, new Observer<OperateResult<Boolean>>() {
            @Override
            public void onChanged(@Nullable OperateResult<Boolean> booleanOperateResult) {
                if (booleanOperateResult.isSuccess()) {
                    Toast.makeText(ChangeNameActivity.this, getString(R.string.modify_success), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ChangeNameActivity.this, getString(R.string.modify_failure), Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
                finish();
            }
        });
    }

}
